package com.sky.service.userImpl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

            ShoppingCart shoppingCart=new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
            shoppingCart.setUserId(BaseContext.get());

            //查套餐和菜品,条件是菜品或套餐id和对应的口味值
            List<ShoppingCart> list =shoppingCartMapper.list(shoppingCart);
            //如果没有则插入数据,缺少名称name,image图片,number数量,amount商品单价
            if (list==null||list.isEmpty()){
                //如果是菜品,查询菜品数据并填充
                if (shoppingCartDTO.getDishId()!=null){
                    Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                    shoppingCart.setName(dish.getName());
                    shoppingCart.setAmount(dish.getPrice());
                    shoppingCart.setImage(dish.getImage());
                }
                //如果是套餐,查询套餐数据并填充
                else if (shoppingCart.getSetmealId()!=null){
                    SetmealVO setmealVO = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                    shoppingCart.setName(setmealVO.getName());
                    shoppingCart.setAmount(setmealVO.getPrice());
                    shoppingCart.setImage(setmealVO.getImage());
                }
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
            //如果有则数量加1
            else {
                ShoppingCart cart= list.get(0);
                cart.setNumber(cart.getNumber()+1);
                shoppingCartMapper.updateNumber(cart);
            }
    }


    @Override
    public List<ShoppingCart> get() {
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(BaseContext.get())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(BaseContext.get())
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.get());
        //查询获取套餐或菜品数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //数量为1则删除 delete
        //数量大于1则减少1 update
        if (list!=null&&!list.isEmpty()){
            ShoppingCart cart=list.get(0);
            if (cart.getNumber()==1){
                shoppingCartMapper.delete(cart);
            }else {
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumber(cart);
            }
        }else {
            throw new DeletionNotAllowedException(MessageConstant.UNKNOWN_ERROR);
        }
    }
}
